//
//  WHNFifthViewController.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//  榜单-主播榜单推出页

#import "WHNFifthViewController.h"
#import "WHNTableViewCell.h"
#import "WHNView.h"
#import "AFTools.h"
#import <UIImageView+WebCache.h>
#import "WHNList.h"
#import <MJExtension.h>
#import "FPlayViewController.h"
#import <MBProgressHUD.h>
#import "PlayerButton.h"
#import "NSString+StringDate.h"

@interface WHNFifthViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)WHNView *topView;
@property(nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic, strong)MBProgressHUD *progressView;

@end

@implementation WHNFifthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view NightWithType:UIViewColorTypeNormal];
    [self.view addSubview:self.tableView];
    _tableView.tableHeaderView = self.topView;
    [self getTopData];
    [self getDownData];
    [self.view addSubview:self.progressView];
}
#pragma mark 获取数据
-(void)getTopData
{
    NSString *string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/homePage?toUid=%@&device=android", _uidString];
    [AFTools getDataByURLString:string WithDataBlock:^(id data) {
        [_topView.WHNBackImageView sd_setImageWithURL:[NSURL URLWithString:[data objectForKey:@"backgroundLogo"]] placeholderImage:[UIImage imageNamed:@"placeholderCh"]];
        [_topView.WHNCenterImageView sd_setImageWithURL:[NSURL URLWithString:[data objectForKey:@"mobileSmallLogo"]] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
        _topView.UserLabel.text = [data objectForKey:@"nickname"];
        _topView.IndexLabel.text = [data objectForKey:@"personalSignature"];
        _topView.NumberLabel1.text = [NSString stringWithFormat:@"%@", [data objectForKey:@"followings"]];
        CGFloat a = [[data objectForKey:@"followers"] integerValue] / 10000 * 1.0;
        if (a >= 1) {
            _topView.NumberLabel2.text = [NSString stringWithFormat:@"%.1f万", a];
        }
        else {
            _topView.NumberLabel2.text = [NSString stringWithFormat:@"%@", [data objectForKey:@"followers"]];
        }
        _topView.NumberLabel3.text = [NSString stringWithFormat:@"%@", [data objectForKey:@"favorites"]];
    }];
}
#pragma mark 获取下部数据
-(void)getDownData
{
    _dataSource = [NSMutableArray array];
    NSString *string = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/track/%@/1/30?device=android", _uidString];
    [AFTools getDataByURLString:string WithDataBlock:^(id data) {
        _dataSource = [WHNList mj_objectArrayWithKeyValuesArray:[data objectForKey:@"list"]];
        [_tableView  reloadData];
        [_progressView removeFromSuperview];
    }];
}
#pragma mark 菊花图
-(MBProgressHUD *)progressView
{
    if (!_progressView) {
        _progressView = [[MBProgressHUD alloc]initWithView:self.view];
        _progressView.labelText = @"加载中";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        [_progressView NightWithType:UIViewColorTypeNormal];
    }
    return _progressView;
}

#pragma mark - tableview初始化
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;

    }
    return _tableView;
}
-(UIView *)topView
{
    if (!_topView) {
        _topView = [[WHNView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250 * FitHeight)];
        _topView.kind = WHNViewKindTopUserInfo;
    }
    return _topView;
}
//#pragma mark 视图将出现时
//-(void)viewWillDisappear:(BOOL)animated
//{
//    [PlayerButton shareButton].hidden = YES;
//}

#pragma mark - tableview cell样式
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identifier = @"cell";
    WHNTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[WHNTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.kind = WHNTableViewCellKindFour;
    if (_dataSource.count != 0) {
        [cell.MainImageView sd_setImageWithURL:[NSURL URLWithString:[_dataSource[indexPath.row] coverSmall]] placeholderImage:[UIImage imageNamed:@"placeholderZh"]];
        cell.TitleLabel.text = [_dataSource[indexPath.row] title];
        cell.NameLabel.text = [NSString stringWithFormat:@" by %@", [_dataSource[indexPath.row] nickname]];
        CGFloat a = [[_dataSource[indexPath.row] playtimes] integerValue] / 10000 * 1.0;
        if (a >= 1) {
            cell.CountLabel.text = [NSString stringWithFormat:@"%.1f万", a];
        }
        else {
            cell.CountLabel.text = [NSString stringWithFormat:@"%@", [_dataSource[indexPath.row] playtimes]];
        }
        cell.LikeLabel.text = [NSString stringWithFormat:@"%@", [_dataSource[indexPath.row] likes]];
        cell.CommentLabel.text = [NSString stringWithFormat:@"%@", [_dataSource[indexPath.row] comments]];
        WHNList *list = _dataSource[indexPath.row];
        cell.TimeLabel.text = [NSString stringWithTimeString:[NSString stringWithFormat:@"%@", list.duration]];
    }
    
    return cell;
}
#pragma mark cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90 * FitHeight;
}
#pragma mark cell个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}
#pragma mark 点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FPlayViewController *playVC = [[FPlayViewController alloc]init];
    WHNList *list = _dataSource[indexPath.row];
    playVC.trackID = [NSString stringWithFormat:@"%@", list.trackId];
    playVC.trackUid = [NSString stringWithFormat:@"%@", list.uid];
    playVC.albumId = [NSString stringWithFormat:@"%@", list.albumId];
    [self.navigationController pushViewController:playVC animated:YES];
}
//#pragma mark 关注的人,粉丝,赞过的手势方法
//-(void)tapAction
//{
//    
//}





















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
